#include <stdio.h>
#include <stdlib.h>
#include "TK.h"
//#include <stdbool.h>

#define INIT_SIZE 2

void InitStack(st *p){
	p->elem = (char *)malloc(INIT_SIZE*sizeof(char));
	p->top = 0;
	p->stacksize = INIT_SIZE;
}

bool IsFull(st *p){
	return p->top == p->stacksize;
}

void Inc(st *p){
	p->elem = (char *)realloc(p->elem,p->stacksize*2*sizeof(char));
	p->stacksize *= 2;//cannot * = 2 ,because *= is a sole symbol.
}

bool Push(st *p,char val){
	if(IsFull(p))
	Inc(p);
	p->elem[p->top] = val;
	p->top++;
	return true;
}

bool IsEmpty(st *p){
	return p->top == 0;
}

bool GetTop(st *p,char *rtval){
	if(IsEmpty(p))
	return false;
	if(rtval != NULL)
	*rtval = p->elem[p->top-1];
	return true;
}

bool Pop(st *p,char *rtval){
	if(IsEmpty(p))
	return false;
	if(rtval != NULL)
	*rtval = p->elem[p->top-1];
	p->top--;
	return true;
}

void Destroy(st *p){
	free(p->elem);
	p->elem = NULL;
	p->top = 0;
	p->stacksize = 0;
}


